﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Specialized;

namespace Settings
{
    public class Setting
    {
        internal static ProgramSettings ps;
        private static XmlDocument xml = new XmlDocument();

        public struct ProgramSettings
        {
            public string langFilePath;
            public TreeNodeCollection genresTree;
            public StringCollection selectedFolders;
        }

        public static void Init()
        {

            #region Init Of ProgramSettings
            Setting.ps.genresTree = new TreeNode().Nodes;
            Setting.ps.selectedFolders = new StringCollection();
            #endregion

            XmlReaderSettings readerSettings = new XmlReaderSettings();
            XmlReader reader = XmlReader.Create("settings.xml", readerSettings);
            try
            {
                xml.Load(reader);

                Enviroment.Lang = xml.DocumentElement.GetElementsByTagName("LangFile").Item(0).InnerText;

                XmlNodeList list = xml.DocumentElement.GetElementsByTagName("GenreTree").Item(0).ChildNodes;
                BuildTreeNodeCollectionFromXml(list, Setting.Genres.GenreList);
               
                reader.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
                reader.Close();
            }
        }

        public static void SaveSetting()
        {
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            //xmlSettings.OmitXmlDeclaration = true;
            writerSettings.Encoding = Encoding.UTF8;
            XmlWriter writer = XmlWriter.Create("settings.xml", writerSettings);
            try
            {
                xml.DocumentElement.RemoveAll();
                
                //jazykove nastaveni
                XmlElement langFileElement = xml.CreateElement("LangFile");
                langFileElement.AppendChild(xml.CreateTextNode(Enviroment.Lang));
                xml.DocumentElement.AppendChild(langFileElement);

                //strom zanru
                XmlElement treeElement = xml.CreateElement("GenreTree");
                BuildXmlTreeFromTreeNodeCollection(Setting.ps.genresTree,treeElement);
                xml.DocumentElement.AppendChild(treeElement);

                xml.Save(writer);
                writer.Flush();
                writer.Close();

            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
                writer.Close();
            }

        }

        private static void BuildXmlTreeFromTreeNodeCollection(TreeNodeCollection nodes,XmlElement element)
        {
            foreach (TreeNode n in nodes)
            {
                XmlElement treeNode = xml.CreateElement("treenode");
                treeNode.SetAttribute("text", n.Text);
                treeNode.SetAttribute("tag", (n.Tag == null)? "" : n.Tag.ToString());
                treeNode.SetAttribute("imagestateindex", n.StateImageIndex.ToString());
                
                if (n.Nodes.Count > 0)
                {
                    BuildXmlTreeFromTreeNodeCollection(n.Nodes,treeNode);
                }

                element.AppendChild(treeNode);
            }
        }

        private static void BuildTreeNodeCollectionFromXml(XmlNodeList list,TreeNodeCollection nodes)
        {
            foreach (XmlElement n in list)
            {
                TreeNode node = new TreeNode();
                node.Text = n.GetAttribute("text");
                node.Tag = n.GetAttribute("tag");
                node.StateImageIndex = Convert.ToInt32((n.GetAttribute("imagestateindex") == "") ? "0" : n.GetAttribute("imagestateindex"));
                
                if (n.HasChildNodes)
                {
                    BuildTreeNodeCollectionFromXml(n.ChildNodes, node.Nodes);
                }

                nodes.Add(node);
            }
                
        }

        public static class Enviroment
        {

            public static string Lang
            {
                get
                {
                    return Setting.ps.langFilePath;
                }
                set
                {
                    Setting.ps.langFilePath = value;
                }
            }
        }

        public static class Genres
        {
            public static TreeNodeCollection GenreList
            {
                get
                {
                    if (Setting.ps.genresTree.Count > 0)
                    {
                        TreeNode node = (TreeNode)Setting.ps.genresTree[0].Clone();
                        return node.Nodes;
                    } else {
                        return Setting.ps.genresTree;
                    }
                }
                set
                {
                    Setting.ps.genresTree = value;
                }
            }

            public static TreeNode[] GetTreeNodeArrayFromTreeNodeCollection(TreeNodeCollection nodes)
            {
                TreeNode[] nodesArray = new TreeNode[nodes.Count];
                nodes.CopyTo(nodesArray, 0);
                return (TreeNode[])nodesArray.Clone();
            }
        }

        public static class Folders
        {
            public static StringCollection SelectedFolders
            {
                get
                {
                    return Setting.ps.selectedFolders;
                }
                set
                {
                    Setting.ps.selectedFolders = value;
                }
            }
        }
    }
}
